<?php

// Namespace

namespace Loop;


/**
 * CExtensionManager allows to manage extensions used by the application
 */

class CExtensionManager
{
	// Properties
	
	private $pathsToExtensions = null;
	private $extensionsIds = null;
	
	
	/**
	 * Constructs an extension manager object
	 */
	
	public function __construct()
	{
		// Create properties
		
		$this->pathsToExtensions = array();
		$this->extensionsIds = array();
	}
	
	
	/**
	 * Returns the paths to all extensions
	 *
	 * @param	bool 	$addPathToApplication 	True to add the path to the application, false otherwise
	 *
	 * @return	array	The path to each extension
	 */
	
	public function getPathsToExtensions($addPathToApplication = true)
	{
		$paths = $this->pathsToExtensions;
		
		if ($addPathToApplication == true)
		{
			array_push($paths, PATH_APPLICATION);
		}
		
		
		return $paths;
	}
	
	
	/**
	 * Returns the Ids/names of all extensions
	 *
	 * @param	bool 	$addApplication 	True to add the application, false otherwise
	 *
	 * @return	array 	The name of each extension
	 */
	
	public function getExtensionsIds($addApplication = true)
	{
		$names = $this->extensionsIds;
		
		if ($addApplication == true)
		{
			array_push($names, EXTENSION_APPLICATION);
		}
		
		return $names;
	}
	
	
	/**
	 * Initializes the extension manager
	 */
	
	public function initialize()
	{
		// Should we follow a specific order for extensions or not?
		
		$pathToExtensionsIds = PATH_APPLICATION . 'Preference/Extensions.txt';
		
		if (file_exists($pathToExtensionsIds) == true)
		{
			// Yes, get the list of extensions IDs
			
			$rawExtensionsIds = file_get_contents($pathToExtensionsIds);
			$extensionsIds = explode(',', $rawExtensionsIds);
			
			
			// Build paths to extensions
			
			$pathToFiles = array();
			
			foreach ($extensionsIds as $extensionId)
			{
				$pathToFiles[] = PATH_EXTENSION . $extensionId;
			}
		}
		else
		{
			// List all extensions under PATH_EXTENSION
			
			$pathToFiles = app()->getFileHelper()->listFiles(PATH_EXTENSION, '*');
		}
		
		
		// Paths from which we will load models
		
		foreach ($pathToFiles as $pathToFile)
		{
			// Skip if not a directory
			
			if (is_dir($pathToFile) == false)
			{
				continue;
			}
			
			
			// Get the name of the extension
			
			$fragments = explode('/', $pathToFile);
			
			$extensionId = array_pop($fragments);
			array_push($this->extensionsIds, $extensionId);
			
			$constantName = 'PATH_' . strtoupper(str_replace('-', '_', $extensionId));
			
			
			// Add a trailing slash
			
			$pathToFile .= '/';
			
			
			// Define a constant (e.g. PATH_LOOP)
			
			cons($constantName, $pathToFile);


			// Try to include proxy functions for this extension

			$pathToProxy = $pathToFile . 'Class/Proxy.php';

			if (file_exists($pathToProxy) == true)
			{
				require_once($pathToProxy);
			}
			
			
			// Create a simple constant with the ID of the extension
			
			$constantName = 'EXTENSION_' . strtoupper(str_replace('-', '_', $extensionId));
			cons($constantName, $extensionId);
			
			
			// Store the path to extension
			
			if ($extensionId != 'Loop')
			{
				array_push($this->pathsToExtensions, $pathToFile);
			}
		}
		
		
		// Make sure that Loop is the first in the array
		
		array_unshift($this->pathsToExtensions, PATH_LOOP);
	}
}

?>
